package com.whitedust.top60;

import java.util.Arrays;

/**
 *
 * @author A
 * @since 2025/11/10 16:37
 */

public class Top55 {
    public String minWindow(String s, String t) {
        //初始化哈希表
        int[] sHash = new int[128];
        int[] tHash = new int[128];
        Arrays.fill(sHash, 0);
        Arrays.fill(tHash, 0);
        //方便取字符
        char[] cs = s.toCharArray();
        for (int i = 0; i < t.length(); i++) {
            tHash[t.charAt(i)]++;
        }
        int ansLeft = -1;
        int ansRight = s.length();
        int i = 0;
        //窗口滑动
        for (int j = 0; j < s.length(); j++) {
            sHash[cs[j]]++;
            while (check(sHash, tHash)) {
                if (j - i < ansRight - ansLeft) {
                    ansRight = j;
                    ansLeft = i;
                }
                sHash[cs[i]]--;
                i++;
            }
        }
        return ansLeft < 0 ? "" : s.substring(ansLeft, ansRight + 1);
    }

    public boolean check(int[] s, int[] t) {
        for (int i = 0; i < s.length; i++) {
            if (s[i] - t[i] < 0) return false;
        }
        return true;
    }
}
